<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Universitas extends CI_Controller {
	
	function __construct(){
		parent::__construct();
		$this->load->model('mdl_universitas');
	}
	
	function index(){
		$this->load->view('master/universitas');
	}
	
	function grid(){
		$data = $this->mdl_universitas->getdata();
		echo $this->mdl_universitas->togrid($data['row_data'], $data['row_count']);
	}
	
	function grid_detail($kd_pt){
		echo $this->mdl_universitas->getdata_detail($kd_pt);
	}
	
	function add(){
		$data['Logo'] =  '';
		$data['kode'] = '';
		$data['kd_pt'] = '';
		$data['Nama'] = '';
		$data['TglMulai'] = '';
		$data['Alamat1'] = '';
		//$data['Alamat2'] = '';
		$data['Kota'] = '';
		$data['KodePos'] = '';
		$data['Telepon'] = '';
		$data['Fax'] = '';
		$data['Email'] = '';
		$data['Website'] = '';
		$data['NoAkta'] = '';
		$data['TglAkta'] = '';
		$data['NoSah'] = '';
		$data['TglSah'] = '';
		$data['status'] = 'A';
		$this->load->view('master/universitas_form', $data);
	}
	
	function edit($kode){
		$r = $this->mdl_universitas->getdataedit($kode);
		
		$data['kode'] = $kode;
		$data['kd_pt'] = $r->row()->kd_pt;
		$data['Nama'] = $r->row()->Nama;
		$data['TglMulai'] = FormatDateFromMysql($r->row()->TglMulai);
		$data['Alamat1'] = $r->row()->Alamat1;
		//$data['Alamat2'] = $r->row()->Alamat2;
		$data['Kota'] = $r->row()->Kota;
		$data['KodePos'] = $r->row()->KodePos;
		$data['Telepon'] = $r->row()->Telepon;
		$data['Fax'] = $r->row()->Fax;
		$data['Email'] = $r->row()->Email;
		$data['Website'] = $r->row()->Website;
		$data['NoAkta'] = $r->row()->NoAkta;
		$data['TglAkta'] = FormatDateFromMysql($r->row()->TglAkta);
		$data['NoSah'] = $r->row()->NoSah;
		$data['TglSah'] = FormatDateFromMysql($r->row()->TglSah);
		$data['status'] = $r->row()->status;
		
		$this->load->view('master/universitas_form', $data);
	}
	
	function save($aksi){
		# init
		$status = "";
		$result = false;
		$data['pesan_error'] = '';
		
		# get post data
		foreach($_POST as $key => $value){
			$data[$key] = $value;
		}
		$data['Logo'] = '';
		
		# rules validasi form
		$this->form_validation->set_rules("kd_pt", 'Kode universitas', 'trim|required|xss_clean');
		$this->form_validation->set_rules("Nama", 'Nama universitas', 'trim|required|xss_clean');
		$this->form_validation->set_rules("Alamat1", 'Alamat universitas', 'trim|required|xss_clean');
		$this->form_validation->set_rules("Kota", 'Kota universitas', 'trim|required|xss_clean');
		$this->form_validation->set_rules("Telepon", 'Telepon universitas', 'trim|required|Email|xss_clean');
		$this->form_validation->set_rules("Email", 'Email universitas', 'trim|required|xss_clean');
		$this->form_validation->set_rules("Website", 'Website universitas', 'trim|required|xss_clean');
		
		# message rules
		$this->form_validation->set_message('required', 'Field %s harus diisi.');

		$data['pesan_error'] = '';
		if ($this->form_validation->run() == FALSE){
			//$data["pesan_error"] .= trim(form_error('kd_pt',' ',' '))==''?'':form_error('kd_pt',' ',' ').'<br>';
			$data["pesan_error"] .= trim(validation_errors(' ',' '))==''?'':validation_errors(' ',' ');
		}else{
			# upload logo
			if (!empty($_FILES['userfile']['name'])){ // jika ada file yg akan diupload
				$config['upload_path'] = './file_upload/images/';
				$config['allowed_types'] = 'gif|jpg|png';
				$config['overwrite'] = TRUE;
				$config['max_size']	= '2000';
				$this->load->library('upload', $config);
				
				if (!$this->upload->do_upload()){
					// $error = array('error' => $this->upload->display_errors());
					// $this->load->view('form_upload', $error);
				}else{
					$up = $this->upload->data();
					$data['Logo'] = $up['file_name'];
				}
			}
			
			if($aksi=="add"){ // add
				$result = !$this->mdl_universitas->isExistKode($data['kd_pt']);
				if ($result){
					$result = $this->mdl_universitas->InsertOnDb($data);
				}else{
					$data['pesan_error'] .= 'Sudah terdapat kode dalam database!';
				}
			}else { // edit
				if($data['kode'] == $data['kd_pt']){ // jika kode tidak diubah
					$result=$this->mdl_universitas->UpdateOnDb($data);
				}else{
					$result = !$this->mdl_universitas->isExistKode($data['kd_pt']);
					if ($result){
						$result=$this->mdl_universitas->UpdateOnDb($data);
					}else{
						$data['pesan_error'] .= 'Sudah terdapat kode dalam database!';
					}
				}
			}
		}
		
		if($result){
			echo json_encode(array('success'=>true));
		}else{
			echo json_encode(array('msg'=>$data['pesan_error']));
		}
	}
	
	function delete($kode){
		$result = $this->mdl_universitas->DeleteOnDb($kode);
		if ($result){
			echo json_encode(array('success'=>true));
		} else {
			echo json_encode(array('msg'=>'Data gagal dihapus'));
		}
	}
	
}